Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MkHtmlTable

github Show source on GitHub

Class representing a html table.

Note

Description

Compared to MkTable, this will end up with a more verbose output, but it can contain more complex Markdown in cells.

Examples

Example: Regular

Note

Jinja

{% set code_col = [_mk.MkCode("print('hello world')
sys.exit()"), _mk.MkCode("print('hello world')
sys.exit()")] %}
{% set admonitions = [_mk.MkAdmonition("Admonition inside cell"), _mk.MkAdmonition("Admonition inside cell")] %}
{% set tabs = [_mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"])), _mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"]))] %}
{% set data = {"Code": code_col, "Admonitions": admonitions, "Tabs": tabs} %}
{{ data | MkHtmlTable }}

Note

Python

MkHtmlTable(
    data={
        "Admonitions": [MkAdmonition(None), MkAdmonition(None)],
        "Code": [MkCode(None), MkCode(None)],
        "Tabs": [MkTabbed(tabs=[...]), MkTabbed(tabs=[...])],
    }
)

Rendered
CodeAdmonitionsTabs
print('hello world')
sys.exit()

Note

Admonition inside cell

A

Tab a

B

Tab b

print('hello world')
sys.exit()

Note

Admonition inside cell

A

Tab a

B

Tab b

Markdown
<table>
<tr><th>Code</th><th>Admonitions</th><th>Tabs</th></tr>
<tr>
<td>

````python
print('hello world')
sys.exit()
````

</td>
<td>

> [!NOTE]
> Admonition inside cell

</td>
<td>

<details open>
<summary>A</summary>

Tab a

</details>

<details>
<summary>B</summary>

Tab b

</details>

</td>
</tr>
<tr>
<td>

````python
print('hello world')
sys.exit()
````

</td>
<td>

> [!NOTE]
> Admonition inside cell

</td>
<td>

<details open>
<summary>A</summary>

Tab a

</details>

<details>
<summary>B</summary>

Tab b

</details>

</td>
</tr>
</table>
Html
<table>
<tr><th>Code</th><th>Admonitions</th><th>Tabs</th></tr>
<tr>
<td>
<pre><code class="language-python">print('hello world')
sys.exit()
</code></pre>
</td>
<td>
<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<p>Admonition inside cell</p>
</div>
</td>
<td>
<details open>
<summary>A</summary>
<p>Tab a</p>
</details>
<details>
<summary>B</summary>
<p>Tab b</p>
</details>
</td>
</tr>
<tr>
<td>
<pre><code class="language-python">print('hello world')
sys.exit()
</code></pre>
</td>
<td>
<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<p>Admonition inside cell</p>
</div>
</td>
<td>
<details open>
<summary>A</summary>
<p>Tab a</p>
</details>
<details>
<summary>B</summary>
<p>Tab b</p>
</details>
</td>
</tr>
</table>

GitHub
<table>
<tr><th>Code</th><th>Admonitions</th><th>Tabs</th></tr>
<tr>
<td>

````python
print('hello world')
sys.exit()
````

</td>
<td>

> [!NOTE]
> Admonition inside cell

</td>
<td>

<details open>
<summary>A</summary>

Tab a

</details>

<details>
<summary>B</summary>

Tab b

</details>

</td>
</tr>
<tr>
<td>

````python
print('hello world')
sys.exit()
````

</td>
<td>

> [!NOTE]
> Admonition inside cell

</td>
<td>

<details open>
<summary>A</summary>

Tab a

</details>

<details>
<summary>B</summary>

Tab b

</details>

</td>
</tr>
</table>
Repr tree
MkHtmlTable
├── MkCode(None)
│   ╰── MkText("print('hello world')\nsys.exit()")
├── MkCode(None)
│   ╰── MkText("print('hello world')\nsys.exit()")
├── MkAdmonition(None)
│   ╰── MkText('Admonition inside cell')
├── MkAdmonition(None)
│   ╰── MkText('Admonition inside cell')
├── MkTabbed(tabs=[...])
│   ├── MkTab(title='A', content='Tab a')
│   │   ╰── MkText('Tab a')
│   ╰── MkTab(title='B', content='Tab b')
│       ╰── MkText('Tab b')
├── MkTabbed(tabs=[...])
│   ├── MkTab(title='A', content='Tab a')
│   │   ╰── MkText('Tab a')
│   ╰── MkTab(title='B', content='Tab b')
│       ╰── MkText('Tab b')
DocStrings

class MkHtmlTable(mkbasetable.MkBaseTable)
Base classes
NameChildrenInherits
MkBaseTable
mknodes.basenodes.mkbasetable
Base Class for MkTables. Only deals with managing the data.
⋔ Inheritance diagram
graph TD
  530410512["mkhtmltable.MkHtmlTable"]
  3678415322["mkbasetable.MkBaseTable"]
  835538629["mkcontainer.MkContainer"]
  1956333208["mkcontainer.MkContainerBase"]
  1359323199["mknode.MkNode"]
  2751680898["builtins.object"]
  3678415322 --> 530410512
  835538629 --> 3678415322
  1956333208 --> 835538629
  1359323199 --> 1956333208
  2751680898 --> 1359323199
NodeFile

/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mkhtmltable/metadata.toml

[metadata]
icon = "octicon:table-24"
status = "new"
name = "MkHtmlTable"

[examples.regular]
title = "Regular"
jinja = """
{% set code_col = [_mk.MkCode("print('hello world')\nsys.exit()"), _mk.MkCode("print('hello world')\nsys.exit()")] %}
{% set admonitions = [_mk.MkAdmonition("Admonition inside cell"), _mk.MkAdmonition("Admonition inside cell")] %}
{% set tabs = [_mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"])), _mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"]))] %}
{% set data = {"Code": code_col, "Admonitions": admonitions, "Tabs": tabs} %}
{{ data | MkHtmlTable }}
"""

[output.markdown]
template = """
{% set table = node.data %}
<table markdown="1">
<tr>
{% for header in table.keys() %}
<td>
{{ header }}
</td>
{% endfor %}
</tr>
{% for row in node.iter_rows() %}
<tr>
{% for item in row %}
<td>
{{ item }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
"""


Code

mknodes.basenodes.mkhtmltable.MkHtmlTable

class MkHtmlTable(mkbasetable.MkBaseTable):
    """Class representing a html table.

    Compared to MkTable, this will end up with a more verbose output,
    but it can contain more complex Markdown in cells.
    """

    STATUS = "new"

    def get_element(self) -> xml.Table | None:
        table_data = self.data  # property
        if not any(table_data[k] for k in table_data):
            return None
        root = xml.Table(markdown=True)
        data = [[str(k) for k in row] for row in self.iter_rows()]
        headers = list(table_data.keys())
        data.insert(0, headers)
        for items in data:
            tr = xml.Tr(parent=root)
            for item in items:
                td = xml.Td(parent=tr)
                td.text = "\n" + item + "\n"
        return root

    def to_md_unprocessed(self) -> str:
        root = self.get_element()
        return root.to_string(space="") if root is not None else ""

Page info
Code for this page

mknodes.manual.nodes_section.create_section_for_nodes

def create_section_for_nodes(
    nav: mk.MkNav,
    klasses: Sequence[type[mk.MkNode]],
) -> mk.MkTable:
    table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
    for kls in klasses:
        tpl = "classpage_custom.jinja"
        page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
        nav += page
        if kls.STATUS:
            page.metadata.status = kls.STATUS
        elif kls.JS_FILES:
            page.metadata.status = "js"
        elif kls.CSS:
            page.metadata.status = "css"
        page.created_by = create_section_for_nodes
        link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
        extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
        table.add_row((link, kls.__doc__, extensions))
    return table

Resources
Resources(css=[],
          markdown_extensions={'admonition': {},
                               'pymdownx.details': {},
                               'pymdownx.highlight': {'anchor_linenums': True,
                                                      'line_spans': '__span',
                                                      'pygments_lang_class': True},
                               'pymdownx.superfences': {'custom_fences': [{'class': 'mermaid',
                                                                           'format': <function fence_code_format at 0x7d707a397060>,
                                                                           'name': 'mermaid'}]},
                               'pymdownx.tabbed': {},
                               'tables': {}},
          plugins=[Plugin('mkdocstrings')],
          js=[],
          assets=[],
          packages=[])
Metadata
created:
  source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/nodes_section.py
  source_function: create_section_for_nodes
  source_line_no: 93
hide:
- toc
icon: octicons/table-24
status: new
title: MkHtmlTable